﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoadTeam.SysInterface;
using System.Data;
using RoadTeam.Model;
using RoadTeam.Enums;

namespace RoadTeam.BsSystem.CS.Maintenance
{
    public class CsMaintenanceDetail_Sys : ICsMaintenanceDetail
    {
        public bool InsertCsMaintenanceDetail(DataTable dt)
        {
            return TbCsMaintenanceDetail.InsertDatatable(dt, GlobalConfig.RtConnectionString);
        }


        public bool InsertCsMaintenanceDetail(DataTable dt, List<string> LiProjectIDs)
        {
            //执行插入TbCsMainProjectRela

            CsProject_Sys project = new CsProject_Sys();
            String StrProjectIDs = string.Empty;
            foreach (String Str in LiProjectIDs)
            {
                StrProjectIDs += Str + ",";
            }
            StrProjectIDs = String.IsNullOrEmpty(StrProjectIDs) ? "-1," : StrProjectIDs;

            DataTable dt_project = project.GetProjectByProjectIDs(StrProjectIDs.TrimEnd(','));
            DataRow[] dr_Projects;

            //ProjectID,ProjectName, MaterialFee, LaborCost, Unit,SortID
            CsMainProjectRela_Sys Real = new CsMainProjectRela_Sys();
            DataTable dt_Rela = Real.GetEmptyTable();
            DataRow dr_Rela;
            //MaintenanceID, ProjectID, ProjectName, MaterialFee, LaborCost, Unit,ClassName,Number,MaterialSum,LaborSum,AllSum

            foreach (DataRow dr in dt.Rows)
            {
                decimal MaterialFee = 0;
                decimal LaborCost = 0;
                dr_Projects = dt_project.Select(@"ProjectID=" + dr["ProjectID"].ToString());
                if (dr_Projects.Count() == 0) continue;
                dr_Rela = dt_Rela.NewRow();
                dr_Rela["ProjectID"] = dr_Projects[0]["ProjectID"];
                dr_Rela["ProjectName"] = dr_Projects[0]["ProjectName"];
                dr_Rela["MaterialFee"] = dr_Projects[0]["MaterialFee"];
                dr_Rela["LaborCost"] = dr_Projects[0]["LaborCost"];
                dr_Rela["Unit"] = dr_Projects[0]["Unit"];
                dr_Rela["MaintenanceID"] = dr["MaintenanceID"];
                dr_Rela["ClassName"] = CsSort_Sys.GetSortNameByID(dr_Projects[0]["SortID"].ToString());
                dr_Rela["MaterialSum"] = dr_Projects[0]["MaterialFee"];
                dr_Rela["LaborSum"] = dr_Projects[0]["LaborCost"];
                dr_Rela["Number"] = 0;


                decimal.TryParse(dr_Projects[0]["MaterialFee"].ToString(), out MaterialFee);
                decimal.TryParse(dr_Projects[0]["LaborCost"].ToString(), out LaborCost);
                dr_Rela["AllSum"] = MaterialFee + LaborCost;

                dt_Rela.Rows.Add(dr_Rela);
            }
            Real.InSertProjectRela(dt_Rela);
            return TbCsMaintenanceDetail.InsertDatatable(dt, GlobalConfig.RtConnectionString);
        }

        public DataTable GetEmptyTable()
        {
            SqlWhere where = new SqlWhere();
            where.SelectColumn = @" Top 0 MaintenanceID,ProjectID,OrganID,CreateDateTime,App_Identifier ";
            return TbCsMaintenanceDetail.Select(where, GlobalConfig.RtConnectionString);
        }
    }
}
